package net.hn.hnms.sys.system.remote;

import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.system.api.domain.SysDictData;
import net.hn.hnms.sys.system.api.domain.vo.SysDictDataTypeVo;
import net.hn.hnms.sys.system.service.ISysDictDataService;
import net.hn.hnms.sys.system.service.ISysDictTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 操作日志记录
 *
 * @author Lion Li
 */
@RequiredArgsConstructor
@Service
public class RemoteDictServiceImpl  {

    private final ISysDictTypeService sysDictTypeService;
    private final ISysDictDataService sysDictDataService;


    public String getDictLabel(String dictType, String dictValue, String separator) {
        StringBuilder propertyString = new StringBuilder();
        List<SysDictData> datas = sysDictTypeService.selectDictDataByType(dictType);

        if (StringUtils.containsAny(dictValue, separator) && CollUtil.isNotEmpty(datas)) {
            for (SysDictData dict : datas) {
                for (String value : dictValue.split(separator)) {
                    if (value.equals(dict.getDictValue())) {
                        propertyString.append(dict.getDictLabel() + separator);
                        break;
                    }
                }
            }
        } else {
            for (SysDictData dict : datas) {
                if (dictValue.equals(dict.getDictValue())) {
                    return dict.getDictLabel();
                }
            }
        }
        return StringUtils.stripEnd(propertyString.toString(), separator);
    }

    public String getDictValue(String dictType, String dictLabel, String separator) {
        StringBuilder propertyString = new StringBuilder();
        List<SysDictData> datas = sysDictTypeService.selectDictDataByType(dictType);

        if (StringUtils.containsAny(dictLabel, separator) && CollUtil.isNotEmpty(datas)) {
            for (SysDictData dict : datas) {
                for (String label : dictLabel.split(separator)) {
                    if (label.equals(dict.getDictLabel())) {
                        propertyString.append(dict.getDictValue() + separator);
                        break;
                    }
                }
            }
        } else {
            for (SysDictData dict : datas) {
                if (dictLabel.equals(dict.getDictLabel())) {
                    return dict.getDictValue();
                }
            }
        }
        return StringUtils.stripEnd(propertyString.toString(), separator);
    }


    /**
     * 根据字典类型获取字典数据列表
     *
     * @param dictType  字典类型
     * @return 字典列表
     */
    public List<SysDictDataTypeVo> getDictList(String dictType) {
        List<SysDictData> sysDictData = sysDictTypeService.selectDictDataByType(dictType);
        return sysDictData.stream().map(t->{
            SysDictDataTypeVo sysDictDataTypeVo = new SysDictDataTypeVo();
            BeanUtils.copyProperties(t, sysDictDataTypeVo);
            return sysDictDataTypeVo;
        }).collect(Collectors.toList());
    }

    public SysDictData queryInfo(String dictType, String dictLabel) {
        return sysDictDataService.queryInfo(dictType,dictLabel);
    }

    public String selectDictLabel(String dictType, String dictValue) {
        return sysDictDataService.selectDictLabel(dictType,dictValue);
    }
}
